/**
 * 
 */
package VBox;

import java.util.Properties;
import java.util.logging.Logger;

import Constants.OSList;

/**
 * @author k4rthikR
 * 
 */
public class VM {
	
	/**
	 * Name of VM
	 */
	String name;
	/**
	 * The RAM of the VM
	 */
	int ram;
	
	/**
	 * Number of cores for the VM
	 */
	int cores;
	/**
	 * Storage of VM in MB's
	 */
	int hdd; 
	
	/**
	 * The OS for the VM
	 */
	OS os;
	Properties settings;
	Logger log = Logger.getAnonymousLogger();
	public String owner;
	
	public VM(Properties props) {
		settings=props;
	}
	
	public void setConfig(String vmName) {
		name = vmName;
	}
	/**
	 * 
	 * @param vmName
	 * @param guestOS
	 * @param vmRam
	 * @param vmCores
	 * @param vmHdd
	 * @return Return code after validating settings
	 */
	public int setConfig(String vmName, OS guestOS, int vmRam, int vmCores, int vmHdd) {
		
		name = vmName;
		hdd  = vmHdd;
		os   = guestOS;
		ram  = vmRam;
		cores= vmCores;
		// return after validating the values
		//return 0;
		return validateSettings(settings);
	}
	
	/**
	 * 
	 * @param settings
	 * @return Return Codes are in the order of parameters for createVM
	 */
	int validateSettings(Properties settings) {
		
		//TODO Check in DB if name already present
		
		
		if(os != OSList.winXP && os != OSList.win7 && os != OSList.ubuntu ) 
			return -2;
		int ramUpperLimit = Integer.parseInt( settings.getProperty("ram.upperlimit","1024") );
		int ramLowerLimit = Integer.parseInt(settings.getProperty("ram.lowerlimit", "128") );
		
		if(ram < ramLowerLimit || ram > ramUpperLimit) 
			return -3;
		
		int coresLowerLimit = Integer.parseInt( settings.getProperty("cores.lowerlimit","4") );
		int coresUpperLimit = Integer.parseInt( settings.getProperty("cores.upperlimit","1") );
		if(cores < coresLowerLimit || cores > coresUpperLimit) 
			return -4;
		
		int hddUpperLimit = Integer.parseInt( settings.getProperty("hdd.upperlimit","4096") ); 
		int hddLowerLimit = Integer.parseInt( settings.getProperty("hdd.lowerlimit","768") ); 
		if(hdd < hddLowerLimit || hdd > hddUpperLimit) 
			return -5;
		
		
		log.info("Valid config for VM");
		listConfig();
		return 0;
	}
	
	public void listConfig() {
		log.info("Vbox ["+name+"] ["+ram+"MB Ram, "+cores+" core(s), "+hdd+"MB hdd, "+os.OSName+"]");
	}
}
